package com.powernode.controller;

import com.powernode.framework.Result;
import com.powernode.framework.ResultBean;
import com.powernode.framework.ResultUtil;
import com.powernode.pojo.DailyReport;
import com.powernode.service.DailyReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: ycc
 * @Date: 2023/2/14 12:14
 */

@RestController
@RequestMapping("/api/dailyReport")
public class DailyReportController {
    @Autowired
    DailyReportService dailyReportService;


    @PostMapping("/update")
    public Result<String> update(@RequestBody DailyReport dailyReport) {
        if(dailyReport.getId()!=null){
            System.out.println(dailyReport.toString());
            dailyReportService.updateTodayReport(dailyReport);
        }else{
            return ResultUtil.fail();
        }
        return ResultUtil.ok();
    }
    @PostMapping("/remove")
    public Result<String> remove(@RequestBody Integer[] ids) {
        dailyReportService.delete(ids);
        return ResultUtil.ok();
    }

    @GetMapping("/list")
    public ResultBean<List<DailyReport>> getPageResult(
            @RequestParam(required = false) String studentNumber,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Date submitTime,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit) {

        Map<String, Object> param = new HashMap<>(16);

        // 计算起始行号
        int offset = (page - 1) * limit;
        int rows = limit;

        param.put("studentNumber", studentNumber);
        param.put("name",name);
        param.put("submitTime", submitTime);
        param.put("offset", offset);
        param.put("rows", rows);

        // 统计记录数
        int totalRows = dailyReportService.count(param);

        // 获取当前页结果集
        List<DailyReport> entities = dailyReportService.getPageResult(param);

        return new ResultBean(0, "查询成功", totalRows, entities);

    }

}
